package com.wandian.news.mvvm.lifetool;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.JsonObject;
import com.mylib.libcore.data.TTBaseAppApi;
import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.network.rx.RxResultHelper;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.news.bean.LifeOnlyContentBean;

import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.mylib.libcore.config.AppConfig.TOUTIAO_KEY;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_LIFE_JK_XTS;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_LIFE_XJQ;


/**
 * @author Administrator
 * @date 2019/12/18-16:11
 * @description LifeToolActivityViewModel
 **/
public class LifeToolActivityViewModel extends BaseViewModel {
    private TTBaseAppApi baseAppApi;
    public MutableLiveData<String> textJQ =  new MutableLiveData<>();
    public MutableLiveData<String> textXTS =  new MutableLiveData<>();
    public LifeToolActivityViewModel(@NonNull Application application) {
        super(application);
        baseAppApi =  RetrofitUtil.getTouTiaoRetrofit().create(TTBaseAppApi.class);
    }
    public void getDataJQ(){
        JsonObject jsonObject = new JsonObject();
            HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("key",TOUTIAO_KEY);
        RxResultHelper.getHttpObservableNothing(getLifecycleProvider(), baseAppApi.getTouTiaoList(NEWS_LIFE_XJQ,hashMap,jsonObject))
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Object o) {
                        Log.d("getTouTiaoList-xjq", JsonUtil.toJson(o)+"");
                        if(!UIUtils.isEmpty(o)){
                            LifeOnlyContentBean bean = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),LifeOnlyContentBean.class);
                            if(!UIUtils.isEmpty(bean) && bean.getCode()==200 && bean.getNewslist().size()>0){
                                textJQ.setValue(bean.getNewslist().get(0).getContent());
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
    public void getDataXTS(){
        JsonObject jsonObject = new JsonObject();
            HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("key",TOUTIAO_KEY);
        RxResultHelper.getHttpObservableNothing(getLifecycleProvider(), baseAppApi.getTouTiaoList(NEWS_LIFE_JK_XTS,hashMap,jsonObject))
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Object o) {
                        Log.d("getTouTiaoList-xjq", JsonUtil.toJson(o)+"");
                        if(!UIUtils.isEmpty(o)){
                            LifeOnlyContentBean bean = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),LifeOnlyContentBean.class);
                            if(!UIUtils.isEmpty(bean) && bean.getCode()==200 && bean.getNewslist().size()>0){
                                textXTS.setValue(bean.getNewslist().get(0).getContent());
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
